package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UacGroupUser;
import com.csun.cmny.provider.model.domain.UacRoleUser;
import com.csun.cmny.provider.model.domain.UacSupporter;
import com.csun.cmny.provider.model.domain.UacUser;
import com.csun.cmny.provider.model.dto.supporter.SupporterCompleteDto;
import com.csun.cmny.provider.model.dto.supporter.SupporterSaveDto;
import com.csun.cmny.provider.model.enums.UacUserStatusEnum;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.EmilUtil;
import com.csun.cmny.provider.utils.Md5Util;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

@RestController
@RequestMapping("/supporter")
public class UacSupporterController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private UacUserService userService;
    @Resource
    private UacRoleUserService roleUserService;
    @Resource
    private UacGroupUserService groupUserService;
    @Resource
    private UacSupporterService supporterService;


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "账号注册")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid SupporterSaveDto supporterSaveDto, BindingResult br) throws UnknownHostException {

        /*校验两次输入的密码是否一致*/
        if (!supporterSaveDto.getLoginPwd().equals(supporterSaveDto.getAffirmLoginPwd())){

            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("supporter.password.error", null));
        }


        /*校验邮箱是否被注册过*/
        UacUser uacUser = new UacUser();
        uacUser.setLoginName(supporterSaveDto.getEmail());
        List<UacUser> select = userService.select(uacUser);
        if(PublicUtil.isNotEmpty(select)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("supporter.email.error", null));
        }

        /*校验此邮箱作为登陆名是否使用过*/
        UacUser uacUser1 = new UacUser();
        uacUser1.setEmail(supporterSaveDto.getEmail());
        List<UacUser> select1 = userService.select(uacUser1);
        if(PublicUtil.isNotEmpty(select1)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("supporter.email.error", null));
        }

        /*校验手机号是否被注册过*/
        UacUser uacUser2 = new UacUser();
        uacUser2.setMobileNo(supporterSaveDto.getMobileNo());
        List<UacUser> select2 = userService.select(uacUser2);
        if(PublicUtil.isNotEmpty(select2)){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("supporter.mobileNo.error", null));
        }

        // 新增账号
        UacUser user = new UacUser();
        user.setLoginName(supporterSaveDto.getEmail());
        user.setLoginPwd(Md5Util.encrypt(supporterSaveDto.getLoginPwd())
        );
        user.setMobileNo(supporterSaveDto.getMobileNo());
        user.setEmail(supporterSaveDto.getEmail());

        user.setRoleCode("supporter");

        int save = userService.save(user);

        //发送激活邮件
        InetAddress addr = InetAddress.getLocalHost();
        EmilUtil.saveEmil(supporterSaveDto.getEmail(),user.getId().toString(),cmnyMessageSource.getMessage("email.this.activate",null),
                "'>http://"+addr.getHostAddress()+":7009/uac/api/activationEmail?userId=",cmnyMessageSource.getMessage("email.theme.activate",null));


        //新增用户和角色的绑定关系
        UacRoleUser uacRoleUser = new UacRoleUser();
        uacRoleUser.setUserId(user.getId());
        uacRoleUser.setRoleId(23L);
        roleUserService.save(uacRoleUser);

        //新增用户和机构的绑定关系
        UacGroupUser uacGroupUser=new UacGroupUser();
        uacGroupUser.setUserId(user.getId());
        uacGroupUser.setGroupId(1034L);
        groupUserService.save(uacGroupUser);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("email.please.activate", null),save);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "完善资料")
    @RequestMapping(value = "/complete", method = RequestMethod.POST)
    public Wrapper complete(@RequestBody @Valid SupporterCompleteDto supporterCompleteDto, BindingResult br) {

        UacSupporter uacSupporter = new UacSupporter();
        BeanUtils.copyProperties(supporterCompleteDto,uacSupporter);

        int save = supporterService.save(uacSupporter);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),save);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "用户重置密码")
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    public Wrapper resetPassword(Long userId) throws UnknownHostException {

        InetAddress addr = InetAddress.getLocalHost();

        UacUser user = userService.selectByKey(userId);



        EmilUtil.saveEmil(user.getEmail(),userId.toString(),cmnyMessageSource.getMessage("email.this.reset",null),
                "'>http://"+addr.getHostAddress()+":7009/uac/api/resetPassword?userId=", cmnyMessageSource.getMessage("email.theme.reset",null));


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("email.password.please.reset", null));
    }
}
